package org.alis.rpc.route.impl;

import org.alis.rpc.common.protocol.RpcProtocol;
import org.alis.rpc.handler.RpcClientHandler;
import org.alis.rpc.route.AbstractRpcLoadBalance;

import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;


/**
 * RpcLoadBalanceRoundRobin desc: 轮询
 *
 * @author luochuan
 * @date 2020/9/30
 */
public class RpcLoadBalanceRoundRobin<V> extends AbstractRpcLoadBalance<V> {
    private AtomicInteger roundRobin = new AtomicInteger(0);

    @Override
    public RpcProtocol doRoute(String serviceKey, List<RpcProtocol> addressList) {
        int size = addressList.size();
        // Round robin
        int index = (roundRobin.getAndAdd(1) + size) % size;
        return addressList.get(index);
    }


}
